package com.powerunion.datacollection.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.TargetDao;
import com.powerunion.datacollection.model.Target;
import com.powerunion.datacollection.model.TargetDetails;
import com.powerunion.datacollection.model.jsonObject.TargetJSONObject;
import com.powerunion.datacollection.model.query.TargetQuery;
import com.powerunion.util.log.LogHelper;

public class TargetManager {
	private static Logger logger = LogHelper.getInstance().getLogger(TargetManager.class);
	private TargetDao dao = null;
	public TargetManager() {
		super();
		this.dao = (TargetDao)DaoFactory.getInstance().getDao(TargetDao.class);
	}
	
	public String save(String saveStr) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		Map map = new HashMap();
		map.put("targetDetails", TargetDetails.class);
		
		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		Target target = (Target)JSONObject.toBean(jsonObject,Target.class,map);

		return this.dao.save(target);
		
	}
	
	public boolean delete(String[] codes) throws Exception{
		//参数为空
		if(codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.delete(codes);
	}
	
	public Target findByKey(String id){
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getBykey(id);
	}
	
	public String getTargets(TargetQuery query,int start,int limit) {
		//参数为空
		if(query == null) {
			String[] args = { "query" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter query NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		
		String results = this.dao.getTargets(query,start,limit);

		return results;
	}
	
	public String getTargetDetails(String targetId) throws DaoException {
		//参数为空
		if(StringUtils.isEmpty(targetId)) {
			String[] args = { "targetId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter targetId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		List list = this.dao.findTargetDetails(targetId);
		JSONArray jsonArray = JSONArray.fromObject(list);
		String result = "{results:"+jsonArray+"}";
		return result;
		
	}
	
	public String loadInfo(String id) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		Target target = this.dao.getBykey(id);
		TargetJSONObject tgjo = new TargetJSONObject();
		
		BeanUtils.copyProperties(tgjo,target);
		
		JSONObject jsonObject = JSONObject.fromObject(tgjo);
		String info = "{success:true,data:["+jsonObject+"]}";
		
		return info;
		
	}
	
	public JSONArray getTargetsTree(String paraSeq) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(paraSeq)) {
			String[] args = { "paraSeq" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter paraSeq NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getTargetsTree(paraSeq);
	}
	
	public List findTargetDetails(String targetId) throws DaoException {
		//参数为空
		if(StringUtils.isEmpty(targetId)) {
			String[] args = { "targetId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter targetId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.findTargetDetails(targetId);
	}
	
	public JSONArray getSelectTargetsTree(String paraSeq) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(paraSeq)) {
			String[] args = { "paraSeq" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter paraSeq NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getSelectTargetsTree(paraSeq);
	}
	
	public JSONArray getTargetFieldTree(String paraSeq) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(paraSeq)) {
			String[] args = { "paraSeq" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter paraSeq NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getTargetFieldTree(paraSeq);
	}
}
